package com.wanxi.dao.Impl;

import com.wanxi.dao.NewsTypeDao;
import com.wanxi.model.NewsType;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsTypeDaoImpl implements NewsTypeDao {
    @Override
    public List<NewsType> findAllNewsType() {
        List<NewsType> list=new ArrayList<>();
        String sql="select * from news_type";
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()){
                list.add(NewsType.builder()
                        .id(query.getInt("id"))
                        .newsTypeName(query.getString("newsTypeName"))
                        .enable(query.getInt("enable"))
                        .createTime(query.getTimestamp("createTime"))
                        .updateTime(query.getTimestamp("updateTime"))
                        .remark(query.getString("remark"))
                        .build());
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public NewsType findNewsTypeById(NewsType newsType) {
        String sql="select * from news_type where id="+newsType.getId();
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()){
                return NewsType.builder()
                        .id(query.getInt("id"))
                        .newsTypeName(query.getString("newsTypeName"))
                        .enable(query.getInt("enable"))
                        .createTime(query.getTimestamp("createTime"))
                        .updateTime(query.getTimestamp("updateTime"))
                        .remark(query.getString("remark"))
                        .build();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public int updateEnable(NewsType newsType) {
        String sql="update news_type set enable="+newsType.getEnable()+",updateTime=(select now()) where id="+newsType.getId();
        return JDBC.update(sql);
    }

    @Override
    public int addNewsType(NewsType newsType) {
        String sql="insert into news_type(newsTypeName,enable,remark,createTime,updateTime) " +
                "values('"+newsType.getNewsTypeName()
                +"',"+newsType.getEnable()
                +",'"+newsType.getRemark()
                +"',(select now()),(select now()))";
        return JDBC.update(sql);
    }

    @Override
    public int deleteNewsType(NewsType newsType) {
        String sql="delete from news_type where id="+newsType.getId();
        int i = JDBC.update(sql);
        if(i==1){
            String updateProductTypeIdSql="update news set newsTypeID = " +
                    "(select id from news_type limit 0,1)" +
                    ",enable=0,remark='类型被删除',updateTime=(select now()) where newsTypeID="+newsType.getId();
            JDBC.update(updateProductTypeIdSql);
        }
        return i;
    }

    @Override
    public int updateNewsType(NewsType newsType) {
        String sql="update news_type set newsTypeName='"+newsType.getNewsTypeName()
                +"',enable="+newsType.getEnable()
                +",remark='"+newsType.getRemark()
                +"',updateTime=(select now()) where id="+newsType.getId();
        return JDBC.update(sql);
    }
}
